﻿<template>
  <div
    class="l-rblock learun-el-form-left"
    style="padding: 16px; overflow: hidden auto"
  >
    <el-form
      :model="formData"
      :rules="rules"
      size="mini"
      labelPosition="left"
      labelWidth="96px"
      ref="form"
    >
      <el-row :gutter="78">
        <div class="l-rblock">
          <el-col :span="24">
            <div class="learun-form-title">{{ $t("基础信息") }}</div>
          </el-col>
          <el-col
            v-if="lr_formLookAuth('case_erp_purchase_f_Number')"
            :span="9"
            style="padding-right: 0"
          >
            <el-form-item
              :label="$t('采购单号')"
              prop="case_erp_purchase_f_Number"
            >
              <l-code
                size="mini"
                code="erp_purchase_num"
                v-model="formData.case_erp_purchase_f_Number"
                v-if="parseInt(formData.case_erp_purchase_f_IsSysNum) == 0"
              />
              <el-input
                v-else
                :disabled="
                  !lr_formEditAuth(
                    'case_erp_apply_f_Number',
                    false || isReadOnly || isEdit
                  )
                "
                v-model="formData.case_erp_apply_f_Number"
              />
            </el-form-item>
          </el-col>
          <el-col
            v-if="lr_formLookAuth('case_erp_purchase_f_IsSysNum')"
            :span="3"
            style="padding-left: 4px"
          >
            <el-form-item
              :label="$t('')"
              prop="case_erp_purchase_f_IsSysNum"
              labelWidth="0px"
            >
              <l-checkbox
                :disabled="
                  !lr_formEditAuth(
                    'case_erp_purchase_f_IsSysNum',
                    false || isReadOnly || isEdit
                  )
                "
                :options="case_erp_purchase_f_IsSysNumOptions"
                v-model="formData.case_erp_purchase_f_IsSysNum"
                @change="handleCodeTypeChange"
              />
            </el-form-item>
          </el-col>
          <el-col
            v-if="lr_formLookAuth('case_erp_purchase_f_ApplyId')"
            :span="9"
            style="padding-right: 0"
          >
            <el-form-item
              :label="$t('关联申请单')"
              prop="case_erp_purchase_f_ApplyId"
            >
              <el-input
                @focus="handleShowApply()"
                readonly
                :disabled="
                  parseInt(formData.case_erp_purchase_f_IsRelated) == 1 || isReadOnly || isEdit
                "
                v-model="formData.case_erp_purchase_f_ApplyId"
              />
            </el-form-item>
          </el-col>
          <el-col
            v-if="lr_formLookAuth('case_erp_purchase_f_IsRelated')"
            :span="3"
            style="padding-left: 4px"
          >
            <el-form-item
              :label="$t('')"
              prop="case_erp_purchase_f_IsRelated"
              labelWidth="0px"
            >
              <l-checkbox
                :disabled="
                  !lr_formEditAuth(
                    'case_erp_purchase_f_IsRelated',
                    false || isReadOnly || isEdit
                  )
                "
                :options="case_erp_purchase_f_IsRelatedOptions"
                v-model="formData.case_erp_purchase_f_IsRelated"
                @change="handleIsRelatedChange"
              />
            </el-form-item>
          </el-col>
          <el-col
            v-if="lr_formLookAuth('case_erp_purchase_f_Theme')"
            :span="24"
          >
            <el-form-item
              :label="$t('订单主题')"
              prop="case_erp_purchase_f_Theme"
            >
              <el-input
                :disabled="
                  !lr_formEditAuth('case_erp_purchase_f_Theme', isReadOnly)
                "
                v-model="formData.case_erp_purchase_f_Theme"
                :placeholder="$t('please enter')"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col
            v-if="lr_formLookAuth('case_erp_purchase_f_PurchaseDate')"
            :span="12"
          >
            <el-form-item
              :label="$t('采购日期')"
              prop="case_erp_purchase_f_PurchaseDate"
            >
              <l-date
                :disabled="
                  !lr_formEditAuth(
                    'case_erp_purchase_f_PurchaseDate',
                    isReadOnly
                  )
                "
                :readonly="false"
                format="yyyy-MM-dd"
                dateType="date"
                :clearable="true"
                v-model="formData.case_erp_purchase_f_PurchaseDate"
                :placeholder="$t('please select')"
              />
            </el-form-item>
          </el-col>
          <el-col
            v-if="lr_formLookAuth('case_erp_purchase_f_SupplierName')"
            :span="12"
          >
            <el-form-item
              :label="$t('供应商名称')"
              prop="case_erp_purchase_f_SupplierName"
            >
              <l-select
                @change="changeSupplier"
                :disabled="
                  !lr_formEditAuth(
                    'case_erp_purchase_f_SupplierName',
                    false || isReadOnly
                  )
                "
                :options="case_erp_purchase_f_SupplierNameOptions"
                v-model="formData.case_erp_purchase_f_SupplierName"
              />
            </el-form-item>
          </el-col>
          <el-col
            v-if="lr_formLookAuth('case_erp_purchase_f_SupplierPerson')"
            :span="12"
          >
            <el-form-item
              :label="$t('联系人')"
              prop="case_erp_purchase_f_SupplierPerson"
            >
              <el-input
                :disabled="
                  !lr_formEditAuth(
                    'case_erp_purchase_f_SupplierPerson',
                    isReadOnly
                  )
                "
                v-model="formData.case_erp_purchase_f_SupplierPerson"
                :placeholder="$t('please enter')"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col
            v-if="lr_formLookAuth('case_erp_purchase_f_SupplierWay')"
            :span="12"
          >
            <el-form-item
              :label="$t('联系方式')"
              prop="case_erp_purchase_f_SupplierWay"
            >
              <el-input
                :disabled="
                  !lr_formEditAuth(
                    'case_erp_purchase_f_SupplierWay',
                    isReadOnly
                  )
                "
                v-model="formData.case_erp_purchase_f_SupplierWay"
                :placeholder="$t('please enter')"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col
            v-if="lr_formLookAuth('case_erp_purchase_f_PurchaseDep')"
            :span="12"
          >
            <el-form-item
              :label="$t('采购部门')"
              prop="case_erp_purchase_f_PurchaseDep"
            >
              <l-department-select
                :disabled="
                  !lr_formEditAuth(
                    'case_erp_purchase_f_PurchaseDep',
                    isReadOnly
                  )
                "
                v-model="formData.case_erp_purchase_f_PurchaseDep"
              />
            </el-form-item>
          </el-col>
          <el-col
            v-if="lr_formLookAuth('case_erp_purchase_f_PurchasePerson')"
            :span="12"
          >
            <el-form-item
              :label="$t('采购人员')"
              prop="case_erp_purchase_f_PurchasePerson"
            >
              <l-user-select
                :disabled="
                  !lr_formEditAuth(
                    'case_erp_purchase_f_PurchasePerson',
                    isReadOnly
                  )
                "
                v-model="formData.case_erp_purchase_f_PurchasePerson"
                @change="purchaseUserSelect"
                @userLoaded="purchaseUserSelect"
                isDepartment
                :department="formData.case_erp_purchase_f_PurchaseDep"
              />
            </el-form-item>
          </el-col>
          <el-col
            v-if="lr_formLookAuth('case_erp_purchase_f_PurchasePhone')"
            :span="12"
          >
            <el-form-item
              :label="$t('联系电话')"
              prop="case_erp_purchase_f_PurchasePhone"
            >
              <el-input
                :disabled="
                  !lr_formEditAuth(
                    'case_erp_purchase_f_PurchasePhone',
                    isReadOnly
                  )
                "
                v-model="formData.case_erp_purchase_f_PurchasePhone"
                :placeholder="$t('please enter')"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col
            v-if="lr_formLookAuth('case_erp_purchase_f_RelatedProject')"
            :span="12"
          >
            <el-form-item
              :label="$t('关联项目')"
              prop="case_erp_purchase_f_RelatedProject"
            >
              <l-select
                :disabled="
                  !lr_formEditAuth(
                    'case_erp_purchase_f_RelatedProject',
                    false || isReadOnly
                  )
                "
                :options="case_erp_purchase_f_RelatedProjectOptions"
                v-model="formData.case_erp_purchase_f_RelatedProject"
              />
            </el-form-item>
          </el-col>
          <el-col
            v-if="lr_formLookAuth('case_erp_purchase_f_PayType')"
            :span="12"
          >
            <el-form-item
              :label="$t('结算方式')"
              prop="case_erp_purchase_f_PayType"
            >
              <l-select
                :disabled="
                  !lr_formEditAuth(
                    'case_erp_purchase_f_PayType',
                    false || isReadOnly
                  )
                "
                :options="case_erp_purchase_f_PayTypeOptions"
                v-model="formData.case_erp_purchase_f_PayType"
              />
            </el-form-item>
          </el-col>
          <el-col
            v-if="lr_formLookAuth('case_erp_purchase_f_PayAddress')"
            :span="24"
          >
            <el-form-item
              :label="$t('支付地址')"
              prop="case_erp_purchase_f_PayAddress"
            >
              <el-input
                :disabled="
                  !lr_formEditAuth('case_erp_purchase_f_PayAddress', isReadOnly)
                "
                v-model="formData.case_erp_purchase_f_PayAddress"
                :placeholder="$t('please enter')"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col
            v-if="lr_formLookAuth('case_erp_purchase_f_Description')"
            :span="24"
          >
            <el-form-item
              :label="$t('备注')"
              prop="case_erp_purchase_f_Description"
            >
              <el-input
                type="textarea"
                :disabled="
                  !lr_formEditAuth(
                    'case_erp_purchase_f_Description',
                    isReadOnly
                  )
                "
                v-model="formData.case_erp_purchase_f_Description"
                :placeholder="$t('please enter')"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <div class="learun-form-title">采购物料</div>
          </el-col>
          <el-col :span="12">
            <div style="text-align: right">
              <el-button
                size="mini"
                type="primary"
                @click="handleCase_erp_purchasedetailAddRow"
                >{{ $t("添加") }}</el-button
              >
              <el-button
                size="mini"
                type="danger"
                @click="handleCase_erp_purchasedetailRemoveRow"
                >{{ $t("移除") }}</el-button
              >
            </div>
          </el-col>
          <el-col v-if="lr_formLookAuth('1670914821908_15394')" :span="24">
            <l-edit-table
              :columns="
                lr_formTableAuth(
                  'case_erp_purchasedetail',
                  case_erp_purchasedetail_columns
                )
              "
              isMultiSelect
              :isAddBtn="false"
              :dataSource="case_erp_purchasedetail_data"
              rowKey="f_Id"
              @deleteRow="deleteRow"
              ref="purchasedetail"
              :summaryList="[
                {
                  label: '总量',
                  prop: 'f_Count',
                  decimals: 0,
                },
                {
                  label: '总金额',
                  prop: 'f_AfterTaxAmount',
                },
              ]"
              showSummary2
            >
              <template v-slot:f_Price="scope">
                <el-input
                  size="mini"
                  :disabled="
                    !lr_formTableEditAuth(
                      'case_erp_purchasedetail',
                      'f_Price',
                      isReadOnly
                    )
                  "
                  @input="handleRowChange(scope.row)"
                  v-model="scope.row.f_Price"
                  :placeholder="$t('please enter')"
                />
              </template>
              <template v-slot:f_Count="scope">
                <el-input-number
                  controls-position="right"
                  size="mini"
                  :disabled="
                    !lr_formTableEditAuth(
                      'case_erp_purchasedetail',
                      'f_Count',
                      isReadOnly
                    )
                  "
                  @change="handleRowChange(scope.row)"
                  v-model="scope.row.f_Count"
                />
              </template>
              <template v-slot:f_Discount="scope">
                <el-input
                  size="mini"
                  :disabled="
                    !lr_formTableEditAuth(
                      'case_erp_purchasedetail',
                      'f_Discount',
                      isReadOnly
                    )
                  "
                  @input="handleRowChange(scope.row)"
                  v-model="scope.row.f_Discount"
                />
              </template>
              <template v-slot:f_TaxRate="scope">
                <el-input
                  size="mini"
                  :disabled="
                    !lr_formTableEditAuth(
                      'case_erp_purchasedetail',
                      'f_TaxRate',
                      isReadOnly
                    )
                  "
                  @input="handleRowChange(scope.row)"
                  v-model="scope.row.f_TaxRate"
                />
              </template>
              <template v-slot:f_TaxBreak="scope">
                <el-input size="mini" disabled v-model="scope.row.f_TaxBreak" />
              </template>
              <template v-slot:f_AfterTaxAmount="scope">
                <el-input
                  size="mini"
                  disabled
                  v-model="scope.row.f_AfterTaxAmount"
                />
              </template>
              <template v-slot:f_DeliveryDate="scope">
                <l-date
                  size="mini"
                  :disabled="
                    !lr_formTableEditAuth(
                      'case_erp_purchasedetail',
                      'f_DeliveryDate',
                      isReadOnly
                    )
                  "
                  :readonly="false"
                  format="yyyy-MM-dd"
                  dateType="date"
                  :clearable="true"
                  v-model="scope.row.f_DeliveryDate"
                  :placeholder="$t('please select')"
                />
              </template>
            </l-edit-table>
          </el-col>
          <el-col :span="24">
            <div class="learun-form-title mt-8">附件</div>
          </el-col>
          <el-col v-if="lr_formLookAuth('case_erp_purchase_f_File')" :span="24">
            <el-form-item prop="case_erp_purchase_f_File" labelWidth="0">
              <l-upload
                drag
                maxSize="10"
                sizeType="MB"
                :limit="3"
                :disabled="
                  !lr_formEditAuth(
                    'case_erp_purchase_f_File',
                    false || isReadOnly
                  )
                "
                v-model="formData.case_erp_purchase_f_File"
                showFileList="uploadTheme1"
              />
            </el-form-item>
          </el-col>
        </div>
      </el-row>

      <!-- 采购申请单 -->
      <l-dialog
        :visible.sync="applyVisible"
        :height="800"
        :width="1000"
        :hasBtns="false"
      >
        <l-choose-purchase-apply
          @rowClick="purchaseApplySelect"
          ref="choseApplyForm"
        />
      </l-dialog>

      <!-- 物料添加 -->
      <l-dialog
        :visible.sync="formVisible"
        :height="800"
        :width="1000"
        @ok="handleMaterialSelect"
      >
        <l-choose-material
          :supplierId="formData.case_erp_purchase_f_SupplierName"
          ref="choseForm"
        />
      </l-dialog>
    </el-form>
  </div>
</template>
<script>
const api = window.$api.erpcase.purchase;
const apiApply = window.$api.erpcase.apply;
export default {
  props: {
    isReadOnly: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      applyVisible: false,

      formVisible: false,

      moduleCode: "ERP_PurchaseOrders",
      activeName: "tab0",
      formData: {
        case_erp_purchase_f_Number: "", // 采购单号
        case_erp_purchase_f_IsSysNum: 0, //
        case_erp_purchase_f_ApplyId: "", // 关联申请单
        case_erp_purchase_f_IsRelated: 0, //
        case_erp_purchase_f_Theme: "", // 订单主题
        case_erp_purchase_f_PurchaseDate: "", // 采购日期
        case_erp_purchase_f_SupplierName: "", // 供应商名称
        case_erp_purchase_f_SupplierPerson: "", // 联系人
        case_erp_purchase_f_SupplierWay: "", // 联系方式
        case_erp_purchase_f_PurchaseDep: "", // 采购部门
        case_erp_purchase_f_PurchasePerson: "", // 采购人员
        case_erp_purchase_f_PurchasePhone: "", // 联系电话
        case_erp_purchase_f_RelatedProject: "", // 关联项目
        case_erp_purchase_f_PayType: "", // 结算方式
        case_erp_purchase_f_PayAddress: "", // 支付地址
        case_erp_purchase_f_Description: "", // 备注
        case_erp_purchase_f_File: "", // 附件
      },
      rules: {
        case_erp_purchase_f_Number: [{ required: true, message: "please enter" }],
        case_erp_purchase_f_ApplyId: [
          { required: true, message: "please select", trigger: "null" },
        ],
        case_erp_purchase_f_Theme: [{ required: true, message: "please enter" }],
        case_erp_purchase_f_PurchaseDate: [
          { required: true, message: "please select" },
        ],
        case_erp_purchase_f_PurchasePhone: [
          // {
          //   pattern:
          //     /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/,
          //   message: "please enter手机号",
          // },
        ],
      },
      case_erp_purchasedetail_columns: [
        {
          id: "case_erp_purchasedetail_F_Number",
          prop: "f_Number",
          label: "物料编码",
          minWidth: 100,
        },
        {
          id: "case_erp_purchasedetail_F_Name",
          prop: "f_Name",
          label: "物料名称",
          minWidth: 140,
        },
        {
          id: "case_erp_purchasedetail_F_Model",
          prop: "f_Model",
          label: "规格型号",
          minWidth: 80,
        },
        {
          id: "case_erp_purchasedetail_F_Unit",
          prop: "f_Unit",
          label: "单位",
          minWidth: 64,
          align: "center",
          dataType: "dataSource",
          dataCode: "ERP_Unit",
          valueKey: "f_id",
          labelKey: "f_name",
        },
        {
          id: "case_erp_purchasedetail_F_Price",
          prop: "f_Price",
          label: "单价",
          minWidth: 80,
          required: true,
        },
        {
          id: "case_erp_purchasedetail_F_Count",
          prop: "f_Count",
          label: "数量",
          minWidth: 100,
          required: true,
        },
        {
          id: "case_erp_purchasedetail_F_Discount",
          prop: "f_Discount",
          label: "折扣%",
          minWidth: 80,
          align: "center",
        },
        {
          id: "case_erp_purchasedetail_F_TaxRate",
          prop: "f_TaxRate",
          label: "税率%",
          minWidth: 80,
          align: "center",
          required: true,
        },
        {
          id: "case_erp_purchasedetail_F_TaxBreak",
          prop: "f_TaxBreak",
          label: "税费",
          minWidth: 80,
          align: "center",
          required: true,
        },
        {
          id: "case_erp_purchasedetail_F_AfterTaxAmount",
          prop: "f_AfterTaxAmount",
          label: "税后金额",
          minWidth: 80,
          align: "center",
          required: true,
        },
        {
          id: "case_erp_purchasedetail_F_DeliveryDate",
          prop: "f_DeliveryDate",
          label: "交付日期",
          minWidth: 156,
        },
      ],
      case_erp_purchasedetail_data: [],

      isEdit:false,
    };
  },
  computed: {
    case_erp_purchase_f_IsSysNumOptions() {
      return [{ value: "0", label: "用系统编号" }];
    },
    case_erp_purchase_f_IsRelatedOptions() {
      return [{ value: "1", label: "不关联" }];
    },
    case_erp_purchase_f_SupplierNameOptions() {
      const data = this.lr_DataSource("ERP_Suppliers");
      return this.lr_DataSourceOptions(data, "f_id", "f_name");
    },
    case_erp_purchase_f_RelatedProjectOptions() {
      this.lr_loadDataItem("140");
      return this.lr_dataItemOptions(this.lr_dataItem["140"]);
    },
    case_erp_purchase_f_PayTypeOptions() {
      this.lr_loadDataItem("PurchaseType");
      return this.lr_dataItemOptions(this.lr_dataItem["PurchaseType"]);
    },
  },
  methods: {
    // 单据编码类型变更
    handleCodeTypeChange() {
      this.formData.case_erp_purchase_f_Number = "";
      if (parseInt(this.formData.case_erp_purchase_f_IsSysNum) != 0) {
        this.rules.case_erp_purchase_f_Number.push({
          validator: this.lr_existDbFiled,
          fieldName: "f_Number",
          keyValue: () => {
            return this.formData.case_erp_apply_f_Id;
          },
          tableName: "case_erp_purchase",
          keyName: "f_Id",
          trigger: "null",
        });
      } else {
        this.formData.case_erp_purchase_f_IsSysNum = 1;
        this.rules.case_erp_apply_f_Number.splice(1, 1);
      }
    },

    // 申请关联改变
    handleIsRelatedChange() {
      this.formData.case_erp_purchase_f_ApplyId = "";
      if (parseInt(this.formData.case_erp_purchase_f_IsRelated) == 1) {
        this.rules.case_erp_purchase_f_ApplyId.splice(0, 1);
      } else {
        this.rules.case_erp_purchase_f_ApplyId.push({
          required: true,
          message: "please select",
          trigger: "null",
        });
        this.formData.case_erp_purchase_f_IsRelated = 0;
      }
    },

    // 采购申请单添加
    handleShowApply() {
      if (parseInt(this.formData.case_erp_purchase_f_IsRelated) == 0) {
        this.applyVisible = true;
      }
    },
    // 采购申please select
    async purchaseApplySelect(data) {
      this.formData.case_erp_purchase_f_ApplyId = data.f_Number;
      this.formData.case_erp_purchase_f_Theme = data.f_Theme;
      this.formData.case_erp_purchase_f_PurchaseDep = data.f_ApplyDep;
      this.formData.case_erp_purchase_f_PurchasePerson = data.f_ApplyPerson;
      this.formData.case_erp_purchase_f_RelatedProject = data.f_RelatedProject;
      this.formData.case_erp_purchase_f_Description = data.f_Description;
      

      

      const list = await this.$awaitWraper(
        apiApply.getDetails({ f_ApplyId: data.f_Id })
      );
      this.case_erp_purchasedetail_data = list.map((t) => {
        const row = {
          f_Number: t.f_Number,
          f_Name: t.f_Name,
          f_Model: t.f_Model,
          f_Unit: t.f_Unit,
          f_Price: t.f_Price,
          f_Count: t.f_Count,
          f_TaxRate: 3,
          f_TaxBreak: 0,
          f_AfterTaxAmount: 0,
          f_DeliveryDate:t.f_PayDate
        };
        this.handleRowChange(row);
        return row;
      });

      this.applyVisible = false;
    },

    // 供应商
    changeSupplier(e) {
      this.formData.case_erp_purchase_f_SupplierPerson = e.f_person;
      this.formData.case_erp_purchase_f_SupplierWay = e.f_phone;
    },
    handleCase_erp_purchasedetailAddRow() {
      this.formVisible = true;
    },
    deleteRow({ index }) {
      this.case_erp_purchasedetail_data.splice(index, 1);
    },
    handleCase_erp_purchasedetailRemoveRow() {
      const data = this.$refs.purchasedetail.getSelected();
      data.forEach(({ f_Id }) => {
        const index = this.case_erp_purchasedetail_data.find(
          (t) => t.f_Id == f_Id
        );
        this.case_erp_purchasedetail_data.splice(index, 1);
      });
    },
    async handleMaterialSelect() {
      const data =await this.$refs.choseForm.getSelectData();
      for (let i in data) {
        let point = {};
        point.f_Number = data[i].f_Number;
        point.f_Name = data[i].f_Name;
        point.f_Model = data[i].f_Model;
        point.f_Unit = data[i].f_Unit;
        point.f_TaxRate = 3;
        point.f_DeliveryDate = "";
        if (data[i].f_Price) {
          point.f_Price = data[i].f_Price;
        }
        this.case_erp_purchasedetail_data.push(point);
      }
      this.$refs.choseForm.resetForm();
      this.formVisible = false;
    },

    // 采购人员选择
    purchaseUserSelect(user) {
      if (user) {
        this.formData.case_erp_purchase_f_PurchasePhone =
          user.f_Mobile || user.f_Telephone;
      } else {
        this.formData.case_erp_purchase_f_PurchasePhone = "";
      }
    },

    // 行数据变更
    handleRowChange(row) {
      if (row.f_Price && row.f_Count && row.f_TaxRate) {
        let num = parseFloat(row.f_Price) * row.f_Count;
        if (row.f_Discount) {
          num = (parseFloat(row.f_Discount) * num) / 100;
        }
        row.f_AfterTaxAmount = (num / (1+parseFloat(row.f_TaxRate) / 100)).toFixed(2);
        row.f_TaxBreak  = (num - parseFloat(row.f_AfterTaxAmount)).toFixed(2);        
      } else {
        row.f_TaxBreak = 0;
        row.f_AfterTaxAmount = 0;
      }
    },

    // Reset表单
    resetForm() {
      this.$formClear(this.$refs.form);
      this.activeName = "tab0";
      this.isEdit = false,
      this.case_erp_purchasedetail_data = [];
    },
    // 校验表单
    async validateForm() {
      if (!(await this.$formValidateWraper(this.$refs.form))) {
        return false;
      }
      if (!this.$refs.purchasedetail.validate()) {
        return false;
      }

      // 校验物料数据在当前供应商下是否具有
      if (this.formData.case_erp_purchase_f_SupplierName) {
        const apiSupplier = window.$api.erpcase.supplier;
        let list = await this.$awaitWraper(
          apiSupplier.supplymaterials({
            f_SupplierId: this.formData.case_erp_purchase_f_SupplierName,
          })
        );
        let _list = this.case_erp_purchasedetail_data.filter(
          (t) => list.findIndex(t2 => t2.f_Number == t.f_Number) == -1
        );
        if (_list.length > 0) {
          this.$message({
            type: "error",
            message: `【${_list[0].f_Name}】${this.$t('此物料不在当前供应商供货清单上')}`,
          });
          return false;
        }
      }

      return true;
    },
    // 设置表单数据
    setForm(data) {
      this.isEdit = true;
      this.$setFormData(
        this.formData,
        data.caseErpPurchaseEntity,
        "case_erp_purchase"
      );
      this.case_erp_purchasedetail_data = data.caseErpPurchasedetailList;
    },
    // 获取表单数据
    getForm() {
      const formData = {};
      formData.caseErpPurchaseEntity = this.$getFormData(
        this.formData,
        "case_erp_purchase"
      );
      formData.caseErpPurchasedetailList = this.case_erp_purchasedetail_data;
      return formData;
    },
    async loadFormData({ keyValue /*,params,node*/ }) {
      //  keyValue 流程中相当于流程processId,params 传递参数,node 流程节点
      const data = await this.$awaitWraper(api.get(keyValue));
      if (data) {
        this.setForm(data);
        return true;
      } else {
        return false;
      }
    },
    async saveForm({ keyValue, isEdit /*,code,node*/ }) {
      // isEdit 是否更新数据, keyValue 流程中相当于流程processId,code 表示流程中的操作码,node 流程节点
      const postData = this.getForm();
      if (isEdit) {
        // Edit
        return await this.$awaitWraper(api.update(keyValue, postData));
      } else {
        // Add
        postData.caseErpPurchaseEntity.f_Id = keyValue;
        postData.caseErpPurchaseEntity.f_SaveState = 1;
        postData.caseErpPurchaseEntity.f_AuditState = 1;
        
        return await this.$awaitWraper(api.add(postData));
      }
    },
  },
};
</script>